package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    IUserService userService;

    /**
     * 关注文章作者
     * @param followUserId 作者id
     * @param isFollow 是否已经关注该作者
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        //当前用户未登录
        if (userId==null) return Result.fail("用户未登录！");
        String key = "follow:"+userId;
        //2.判断当前用户是否关注
        //需要关注
        if (isFollow) {
            //将关注信息保存至数据库
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean flag = save(follow);
            //将关注信息保存至redis
            if (flag) stringRedisTemplate.opsForSet().add(key,followUserId.toString());
        }
        //需要取关
        else {
            //移除数据库中对应的关注信息
            LambdaQueryWrapper<Follow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Follow::getUserId,userId).eq(Follow::getFollowUserId,followUserId);
            boolean flag = remove(lambdaQueryWrapper);
            //移除redis中对应的关注信息
            if (flag) stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
        }
        return Result.ok();
    }

    /**
     * 是否关注本文章作者
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        //1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        //当前用户未登录
        if (userId==null) return Result.fail("用户未登录！");
        //2.判断当前用户是否有关注该文章作者
        Integer count = lambdaQuery().eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId).count();
        return Result.ok(count>0);
    }

    /**
     * 查看当前用户和指定用户之间的共同关注的作者
     * @param id 指定用户
     * @return
     */
    @Override
    public Result followCommons(Long id) {
        //1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        //当前用户未登录
        if (userId==null) return Result.fail("用户未登录！");
        //2.求交集
        String key1= "follow:"+userId;
        String key2= "follow:"+id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //无交集的情况
        if (intersect==null ||intersect.isEmpty()) return Result.ok(Collections.emptyList());
        //有交集的情况
        //先把ids转成long型
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //再根据ids返回一个UserDto列表
        List<UserDTO> dtos = userService.listByIds(ids)
                .stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(dtos);
    }
}
